<?php
/**
 * Created by PhpStorm.
 * User: Weizehua
 * Date: 8/7/2016
 * Time: 13:04
 */
class Test
{
    //$evaluatorPath = $_REQUEST['evaluator'];
    //$code = $_REQUEST['code'];
    public $evaluatorPath = 'http://192.168.40.135:81/test/generator.php';
    
    function evaluation($auth, $code)
    {
        // prepare params
        $passingCode = $code;
        $data = $auth . $code;
        $passingHash = md5($data);
        $passingMethod = 'eval';
        
        // construct request
        $params = array(
            'action' => 'eval',
            'code' => $passingCode,
            'hash' => $passingHash,
            'method' => $passingMethod
        );
        $opt = array('http' => array(
            'method' => 'POST',
            'header' => 'Content-Type: application/x-www-form-urlencoded',
            'content' => http_build_query($params)
        ));
        
        // post
        $data = file_get_contents($this->evaluatorPath, false, stream_context_create($opt));
        return $data;
    }
    
    
    function getAuth()
    {
        // construct request
        $params = array(
            'action' => 'authCode'
        );
        $opt = array('http' => array(
            'method' => 'POST',
            'header' => 'Content-Type: application/x-www-form-urlencoded',
            'content' => http_build_query($params)
        ));
        
        // get auth code
        $data = file_get_contents($this->evaluatorPath, false, stream_context_create($opt));
        if ($data)
        {
            $obj = json_decode($data);
            if ($obj)
            {
                $auth = $obj->authCode;
                return $auth;
            }
        }
        return false;
    }
}

$flag = md5(strval(time()) . 'rsa_eval_test_succeed');
$code = "echo '$flag';";
$t = new Test();
$data = $t->evaluation($t->getAuth(), $code);
if(strstr($data, $flag))
{
    echo 'test passed';
}
else
{
    echo "test failed, response : <br>";
    echo $data;
}
